using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class CarSprayItem : BaseItem
	{
		private Collider2D targetObject;

		private BaseItem targetItem;

		private float stayTime;

		private Color32 originalColor;

		private Color32 newColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isDrag)
			{
				RaycastHit2D[] array = Physics2D.BoxCastAll(base.transform.position + new Vector3(-1.5f, 1.17f), new Vector2(3f, 0.8f), 0f, Vector2.zero, 15f, 4096);
				if (array.Length > 1)
				{
					for (int i = 0; i < array.Length - 1; i++)
					{
						for (int num = array.Length - 1; num > i; num--)
						{
							if (array[num - 1].collider.transform.position.z > array[num].collider.transform.position.z)
							{
								RaycastHit2D raycastHit2D = array[num];
								array[num] = array[num - 1];
								array[num - 1] = raycastHit2D;
							}
						}
					}
				}
				bool flag = false;
				for (int j = 0; j < array.Length; j++)
				{
					RaycastHit2D raycastHit2D2 = array[j];
					if (!(raycastHit2D2.collider != null))
					{
						continue;
					}
					ItemComponentTrigger component = raycastHit2D2.collider.GetComponent<ItemComponentTrigger>();
					if (component.type == ItemComponentTrigger.Type.CarColor && component.owner.itemData.scriptType == 24)
					{
						if (targetObject == null || targetObject != raycastHit2D2.collider)
						{
							targetObject = raycastHit2D2.collider;
							targetItem = component.owner;
							originalColor = ((CarItem)targetItem).GetColor(raycastHit2D2.collider.transform);
							stayTime = 0f;
						}
						flag = true;
						break;
					}
				}
				if (targetObject != null && !flag)
				{
					targetObject = null;
					targetItem = null;
					stayTime = 0f;
				}
			}
			if (targetItem != null && stayTime <= 3f)
			{
				stayTime += Time.deltaTime;
				newColor.r = (byte)Mathf.Lerp((int)originalColor.r, (int)base.itemData.color.r, stayTime / 3f);
				newColor.g = (byte)Mathf.Lerp((int)originalColor.g, (int)base.itemData.color.g, stayTime / 3f);
				newColor.b = (byte)Mathf.Lerp((int)originalColor.b, (int)base.itemData.color.b, stayTime / 3f);
				((CarItem)targetItem).ChangeColor(targetObject.transform, newColor);
			}
		}

		protected override void PlayDragEffect(float z = -0.0004f)
		{
			base.PlayDragEffect(z);
			if (dragEfc != null)
			{
				ParticleSystem[] componentsInChildren = dragEfc.GetComponentsInChildren<ParticleSystem>();
				ParticleSystem.MinMaxGradient startColor = default(ParticleSystem.MinMaxGradient);
				startColor.colorMin = base.itemData.color;
				startColor.colorMax = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
				startColor.mode = ParticleSystemGradientMode.TwoColors;
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					ParticleSystem.MainModule main = componentsInChildren[i].main;
					main.startColor = startColor;
				}
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			targetObject = null;
			targetItem = null;
			stayTime = 0f;
			base.TouchUp(touchEventData);
		}
	}
}
